import json
import time
from .getPublicData import *
def getBaseData():
    cars=list(getAllCars())
    #总共获取的汽车总数
    sumCar=len(cars)
    #获取销量最多的数据，直接获取排名第一的，因为爬的是懂车帝销量排行榜
    highVolume=cars[0].salevolume
    topCar=cars[0].carname

    #销售最多车型
    carModels = {}
    maxModel = 0
    mostModel = ''

    for i in cars:
        # 检查 carmodel 是否不在字典中
        if i.carmodel not in carModels:
            carModels[i.carmodel] = 1
        else:
            carModels[i.carmodel] += 1

        # 更新出现次数最多的车型
        if carModels[i.carmodel] > maxModel:
            maxModel = carModels[i.carmodel]
            mostModel = i.carmodel



    carModels=sorted(carModels.items(), key=lambda x: x[1], reverse=True)
    mostModel=carModels[0][0]
        # 打印当前的车型计数字典
    #print(carModels)

    # 最后打印出现次数最多的车型
    #print(f"The most common car model is {mostModel} with {maxModel} occurrences.")

    #最多品牌

    carBrands={}
    maxBrand=0
    mostBrand=''
    for i in cars:
        # 检查 carmodel 是否不在字典中
        if i.brand not in carBrands:
            carBrands[i.brand] = 1
        else:
            carBrands[i.brand] += 1

        # 更新出现次数最多的车型
        if carBrands[i.brand] > maxBrand:
            maxBrand = carBrands[i.brand]
            mostBrand = i.brand



    carBrands=sorted(carBrands.items(), key=lambda x: x[1], reverse=True)
        # 打印当前的车型计数字典
    #print(carBrands)

    # 最后打印出现次数最多的品牌
    #print(f"The most common car model is {mostBrand} with {maxBrand} occurrences.")


    #获取车辆平均价格

    averagePrice=0
    carPrice={}
    sumPrice=0
    for i in cars:
        x=json.loads(i.price)[0]+json.loads(i.price)[1]
        sumPrice+=x
        averagePrice=sumPrice/(sumCar*2)
        averagePrice=round(averagePrice,2)
    #print(averagePrice)

    print(sumCar,highVolume,topCar,mostModel,mostBrand,averagePrice)
    return sumCar,highVolume,topCar,mostModel,mostBrand,averagePrice

def getRollData():
    cars=list(getAllCars())

    #brand
    carBrands = {}
    maxBrand = 0
    mostBrand = ''
    for i in cars:
        # 检查 carmodel 是否不在字典中
        if i.brand not in carBrands:
            carBrands[i.brand] = 1
        else:
            carBrands[i.brand] += 1

        # 更新出现次数最多的车型
        if carBrands[i.brand] > maxBrand:
            maxBrand = carBrands[i.brand]
            mostBrand = i.brand

    #carBrands = sorted(carBrands.items(), key=lambda x: x[1], reverse=True)
    # 打印当前的车型计数字典
    # print(carBrands)

    brandlist=[(value,key) for key,value in carBrands.items()]
    brandlist=sorted(brandlist,reverse=True)[:10]
    sortDict={i[1]:i[0] for i in brandlist}
    lastsortlist=[]
    for k,v in sortDict.items():
        lastsortlist.append({
            'name':k,
            'value':v
        })

    return lastsortlist


def getTypeRate():
    cars=list(getAllCars())
    #能源类型
    carTypes = {}

    for i in cars:
        # 检查 carmodel 是否不在字典中
        if i.energytype not in carTypes:
            carTypes[i.energytype] = 1
        else:
            carTypes[i.energytype] += 1

    oilRate=round(carTypes['汽油']/496*100,2)
    eleRate = round(carTypes['纯电动'] / 496 * 100, 2)
    mixRate=round(((496-carTypes['汽油']-carTypes['纯电动'])/496*100),2)

    print(carTypes)
    print(oilRate,eleRate,mixRate)
    return oilRate,eleRate,mixRate








